﻿namespace HiMap.MapControls.Tools.Frm
{
    using HiMap.Carto;
    using HiMap.MapControls;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;

    internal class FrmInfo : Form
    {
        private bool _isEdit;
        private const int CellPixelH = 0x15;
        private const int CellPixelW = 0x49;
        private System.Windows.Forms.DataGridTextBoxColumn col1;
        private System.Windows.Forms.DataGridTextBoxColumn col2;
        private IContainer components;
        private System.Windows.Forms.DataGridTableStyle data;
        private string FrmInfoCaption;
        private string FrmInfoColumn1;
        private string FrmInfoColumn2;
        private string FrmInfoNodeText;
        private System.Windows.Forms.DataGrid grid;
        private MainMenu mainMenu1;
        private TreeNode node;
        private TreeView tree;
        private TextBox txtValue;

        public FrmInfo(bool isEdit)
        {
            this.InitializeComponent();
            this.getText();
            this.Text = this.FrmInfoCaption;
            DataTable table = new DataTable();
            table.Columns.Add("col1");
            table.Columns.Add("col2");
            this.grid.DataSource = table;
            this.node = new TreeNode();
            this.node.Text = this.FrmInfoNodeText;
            this.tree.Nodes.Add(this.node);
            this._isEdit = isEdit;
            this.txtValue = new TextBox();
            this.txtValue.Visible = false;
            this.txtValue.Text = "";
            this.txtValue.Width = 80;
            this.grid.Controls.Add(this.txtValue);
            this.grid.TableStyles[0].GridColumnStyles[0].HeaderText = this.FrmInfoColumn1;
            this.grid.TableStyles[0].GridColumnStyles[1].HeaderText = this.FrmInfoColumn2;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void getText()
        {
            this.FrmInfoCaption = Setup.GetInfo("FrmInfoCaption");
            this.FrmInfoNodeText = Setup.GetInfo("FrmInfoNodeText");
            this.FrmInfoColumn1 = Setup.GetInfo("FrmInfoColumn1");
            this.FrmInfoColumn2 = Setup.GetInfo("FrmInfoColumn2");
        }

        private void grid_MouseDown(object sender, MouseEventArgs e)
        {
            if (this._isEdit)
            {
                int row = this.grid.HitTest(e.X, e.Y).Row;
                int column = this.grid.HitTest(e.X, e.Y).Column;
                if ((row >= 0) && (column == 1))
                {
                    this.txtValue.Location = new Point(0x49, 0x15 * (row + 1));
                    this.txtValue.Visible = true;
                }
            }
        }

        private void InitializeComponent()
        {
            this.mainMenu1 = new MainMenu();
            this.tree = new TreeView();
            this.grid = new System.Windows.Forms.DataGrid();
            this.data = new System.Windows.Forms.DataGridTableStyle();
            this.col1 = new System.Windows.Forms.DataGridTextBoxColumn();
            this.col2 = new System.Windows.Forms.DataGridTextBoxColumn();
            base.SuspendLayout();
            this.tree.Dock = DockStyle.Left;
            this.tree.Location = new Point(0, 0);
            this.tree.Name = "tree";
            this.tree.Size = new Size(0x4c, 0x10c);
            this.tree.TabIndex = 0;
            this.tree.AfterSelect += new TreeViewEventHandler(this.tree_AfterSelect);
            this.grid.BackgroundColor = Color.FromArgb(0x80, 0x80, 0x80);
            this.grid.Dock = DockStyle.Fill;
            this.grid.Location = new Point(0x4c, 0);
            this.grid.Name = "grid";
            this.grid.Size = new Size(0xa4, 0x10c);
            this.grid.TabIndex = 1;
            this.grid.TableStyles.Add(this.data);
            this.grid.MouseDown += new MouseEventHandler(this.grid_MouseDown);
            this.data.GridColumnStyles.Add(this.col1);
            this.data.GridColumnStyles.Add(this.col2);
            this.data.MappingName = "data";
            this.col1.MappingName = "col1";
            this.col2.MappingName = "col2";
            this.col2.Width = 80;
            base.AutoScaleDimensions = new SizeF(96f, 96f);
            base.AutoScaleMode = AutoScaleMode.Dpi;
            this.AutoScroll = true;
            base.ClientSize = new Size(240, 0x10c);
            base.Controls.Add(this.grid);
            base.Controls.Add(this.tree);
            base.Menu = this.mainMenu1;
            base.Name = "FrmInfo";
            this.Text = "Information";
            base.ResumeLayout(false);
        }

        public void LoadData(IFeature Feat)
        {
            if (Feat != null)
            {
                TreeNode node = new TreeNode();
                node.Text = "1";
                node.Tag = Feat;
                this.node.Nodes.Add(node);
                this.tree.SelectedNode = node;
            }
        }

        public void LoadData(IFeatureCursor cur)
        {
            cur.Flush();
            int num = 1;
            IFeature feature = cur.Next();
            while (feature != null)
            {
                TreeNode node = new TreeNode();
                node.Text = num.ToString();
                node.Tag = feature;
                this.node.Nodes.Add(node);
                feature = cur.Next();
                num++;
            }
            if (this.node.Nodes.Count > 0)
            {
                this.tree.SelectedNode = this.node.Nodes[0];
            }
        }

        private void tree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Parent != null)
            {
                IFeature tag = (IFeature) e.Node.Tag;
                DataTable table = new DataTable("data");
                table.Columns.Add("col1");
                table.Columns.Add("col2");
                for (int i = 0; i < tag.Class.Fields.FieldCount; i++)
                {
                    DataRow row = table.NewRow();
                    row[0] = tag.Class.Fields.GetField(i).Name;
                    row[1] = tag.GetValue(i);
                    table.Rows.Add(row);
                }
                this.grid.DataSource = table;
            }
            else
            {
                DataTable table2 = new DataTable();
                table2.Columns.Add("col1");
                table2.Columns.Add("col2");
                this.grid.DataSource = table2;
            }
        }
    }
}

